<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>HTML + CSS</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <script type="module">
      class Calculator {
        plugins = [];
        constructor(initial) {
          console.log("🚀 ~ Calculator ~ construct ~ initial:", initial);
          this.num = initial;
        }
        use(plugin) {
          this.plugins.push(plugin);
          this[plugin.name] = plugin.calculate.bind(this);
        }
        result() {
          return this.num;
        }
      }

      class AddPlugin {
        constructor() {
          this.name = "add";
        }
        calculate(num) {
          console.log("this.num", this.num);
          this.num += num;
          return this;
        }
      }

      class MinusPlugin {
        constructor() {
          this.name = "minus";
        }
        calculate(num) {
          this.num -= num;
          return this;
        }
      }

      const myCalculator = new Calculator(5);

      // 插件安装
      myCalculator.use(new AddPlugin());
      myCalculator.use(new MinusPlugin());

      console.log("计算结果", myCalculator.add(5).result());
    </script>
  </body>
</html>
